(function() {
  var express, fs, iced, mkdirp, moment, path, pgsql, router, uuid, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  express = require('express');

  pgsql = require('pg');

  uuid = require('uuid');

  moment = require('moment');

  mkdirp = require('mkdirp');

  fs = require('fs');

  path = require('path');

  router = express.Router();

  module.exports = router;

  router.get('/', function(req, res, next) {
    var client, done, err, result, where_clause, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 16
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        where_clause = "( name like $1 or contact like $1 or phone like $1 or address like $1 or notes like $1 )";
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
          });
          client.query("select count(*) as count from pay_p2_fund where " + where_clause, ['%' + res.locals.keyword + '%'], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 27
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          res.locals.total = result.rows[0].count;
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
            });
            client.query("select * from pay_p2_fund where " + where_clause + " order by serial offset $2::int limit $3::int", ['%' + res.locals.keyword + '%', res.locals.offset, res.locals.page_rows], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 37
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            done();
            return res.render('p2/fund/index', {
              records: result.rows
            });
          });
        });
      };
    })(this));
  });

  router.get('/new', function(req, res, next) {
    res.locals.breadcrumbs.push({
      'title': '新增资金池'
    });
    return res.render('p2/fund/new', {
      record: {}
    });
  });

  router.post('/new', function(req, res, next) {
    var client, done, err, k, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    _ref = ['name', 'overdraw', 'cordon', 'agent', 'contact', 'phone'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 52
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
          });
          client.query("insert into pay_p2_fund ( uuid, name, overdraw, cordon, agent, contact, phone, address, email, notes ) values ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10 )", [uuid.v4(), req.body.name, req.body.overdraw, req.body.cordon, req.body.agent, req.body.contact, req.body.phone, req.body.address, req.body.email, req.body.notes], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 72
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          return res.redirect('/p2/fund');
        });
      };
    })(this));
  });

  router.get('/edit/:id', function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.breadcrumbs.push({
      'title': '修改资金池'
    });
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 81
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
          });
          client.query("select * from pay_p2_fund where serial = $1::int or uuid = $2", [parseInt(req.params.id) || 0, req.params.id], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 88
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          if (result.rows.length !== 1) {
            return next(new Error("查无此记录[" + req.params.id + "]"));
          }
          return res.render('p2/fund/edit', {
            record: result.rows[0]
          });
        });
      };
    })(this));
  });

  router.post('/edit', function(req, res, next) {
    var client, done, err, k, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    _ref = ['serial', 'uuid', 'overdraw', 'cordon', 'name', 'agent', 'contact', 'phone'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 102
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
          });
          client.query("update pay_p2_fund set name = $1, overdraw = $2, cordon = $3, agent = $4, contact = $5, phone = $6, address = $7, email = $8, notes = $9 where serial = $10 and uuid = $11", [req.body.name, req.body.overdraw, req.body.cordon, req.body.agent, req.body.contact, req.body.phone, req.body.address, req.body.email, req.body.notes, req.body.serial, req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 122
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          return res.redirect('/p2/fund');
        });
      };
    })(this));
  });

  router.post('/delete', function(req, res, next) {
    var client, done, err, k, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.type('json');
    _ref = ['serial', 'uuid'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 133
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }

        /*
        await client.query \
            "delete from pay_p2_fund_alter where p2_fund = $1", [
                req.body.uuid,
            ], defer err, result
        if err then done(client); return next(err);
         */
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
          });
          client.query("delete from pay_p2_fund where serial = $1 and uuid = $2", [req.body.serial, req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 148
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          return res.json({
            succ: true,
            redirect: '/p2/fund'
          });
        });
      };
    })(this));
  });

  router.get('/recharge/:id', function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.breadcrumbs.push({
      'title': '资金池充值'
    });
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 157
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
          });
          client.query("select * from pay_p2_fund where serial = $1::int or uuid = $2", [parseInt(req.params.id) || 0, req.params.id], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 164
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          if (result.rows.length !== 1) {
            return next(new Error("查无此记录[" + req.params.id + "]"));
          }
          return res.render('p2/fund/recharge', {
            record: result.rows[0]
          });
        });
      };
    })(this));
  });

  router.post('/recharge', function(req, res, next) {
    var client, done, err, fund_history, k, recharge, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    _ref = ['serial', 'uuid', 'recharge'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 177
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        recharge = parseFloat(req.body.recharge);
        if (!recharge) {
          return next(new Error("充值金额 " + req.body.recharge + " 无效"));
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
          });
          client.query("select fund_total, fund_remains, fund_history from pay_p2_fund where serial = $1::int or uuid = $2", [parseInt(req.body.serial) || 0, req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 189
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          if (result.rows.length !== 1) {
            done();
            return next(new Error('未查询到记录'));
          }
          fund_history = result.rows[0].fund_history || [];
          fund_history.push({
            time: moment().format('YYYY-MM-DD HH:mm:ss'),
            user: req.session.userinfo.realname,
            amt: recharge,
            total: result.rows[0].fund_total,
            remains: result.rows[0].fund_remains
          });
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
            });
            client.query("insert into pay_p2_fund_alter ( p2_fund, type, remains, amt, notes ) values ( $1, 1, $2, $3, $4 )", [req.body.uuid, result.rows[0].fund_remains, 0 - recharge, "用户 '" + req.session.userinfo.realname + "' 充值 " + recharge + " 元"], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 213
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
              });
              client.query("update pay_p2_fund set fund_total = fund_total + $1, fund_remains = fund_remains + $1, fund_history = $2 where serial = $3::int or uuid = $4", [recharge, JSON.stringify(fund_history), parseInt(req.body.serial) || 0, req.body.uuid], __iced_deferrals.defer({
                assign_fn: (function() {
                  return function() {
                    err = arguments[0];
                    return result = arguments[1];
                  };
                })(),
                lineno: 224
              }));
              __iced_deferrals._fulfill();
            })(function() {
              if (err) {
                done(client);
                return next(err);
              }
              done();
              return res.redirect('/p2/fund');
            });
          });
        });
      };
    })(this));
  });

  router.all('/recharge/history/:id', function(req, res, next) {
    var client, done, err, max_amt, min_amt, result, where_clause, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.breadcrumbs.push({
      'title': '充值历史记录'
    });
    where_clause = "( h->>'user' like $1 or h->>'amt' like $1 or h->>'time' like $1 )";
    res.locals.start_date = req.body.start_date || req.query.start_date || moment().format('YYYY-MM-DD');
    res.locals.end_date = req.body.end_date || req.query.end_date || moment().format('YYYY-MM-DD');
    where_clause += "and cast(h->>'time' as date) >= '" + res.locals.start_date + "'::date\nand cast(h->>'time' as date) <= '" + res.locals.end_date + "'::date";
    if (req.body.min_amt && req.body.min_amt.length > 0) {
      min_amt = parseFloat(req.body.min_amt) || 0;
      where_clause += " and cast(h->>'amt' as decimal) >= " + min_amt;
      res.locals.min_amt = min_amt.toString();
    }
    if (req.body.max_amt && req.body.max_amt.length > 0) {
      max_amt = parseFloat(req.body.max_amt) || 0;
      where_clause += " and cast(h->>'amt' as decimal) <= " + max_amt;
      res.locals.max_amt = max_amt.toString();
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 256
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
          });
          client.query("select count(h)::int as count from pay_p2_fund as f, json_array_elements(f.fund_history) as h where " + where_clause + " and (serial = $2::int or uuid = $3)", ["%" + res.locals.keyword + "%", parseInt(req.params.id) || 0, req.params.id], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 267
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          res.locals.total = result.rows[0].count;
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
            });
            client.query("select f.name as name, f.agent as agent, f.contact as contact, h->>'time' as time, h->>'user' as user, h->>'amt' as amt, h->>'total' as total, h->>'remains' as remains from pay_p2_fund as f, json_array_elements(f.fund_history) as h where " + where_clause + " and (serial = $2::int or uuid = $3) order by h->>'time' desc offset $4::int limit $5::int", ["%" + res.locals.keyword + "%", parseInt(req.params.id) || 0, req.params.id, res.locals.offset, res.locals.page_rows], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 283
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            done();
            return res.render('p2/fund/history', {
              records: result.rows
            });
          });
        });
      };
    })(this));
  });

  router.all('/alter/:id', function(req, res, next) {
    var client, done, err, result, table_name, today, where_clause, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.breadcrumbs.push({
      'title': '资金变动历史'
    });
    where_clause = "( a.amt::text like $1 or a.merno like $1 or a.termid like $1 or a.notes like $1 )";
    today = moment().format('YYYY-MM-DD');
    res.locals.start_date = req.body.start_date || req.query.start_date || today;
    res.locals.end_date = req.body.end_date || req.query.end_date || today;
    where_clause += "and a.ctime::date >= '" + res.locals.start_date + "'\nand a.ctime::date <= '" + res.locals.end_date + "'";
    table_name = 'pay_p2_fund_alter_hist';
    if (res.locals.start_date === today && res.locals.end_date === today) {
      table_name = 'pay_p2_fund_alter';
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 312
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
          });
          client.query("select count(*)::int as count from " + table_name + " as a where " + where_clause + " and p2_fund = $2", ["%" + res.locals.keyword + "%", req.params.id], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 319
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          res.locals.total = result.rows[0].count;
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
            });
            client.query("select a.*, f.name as fund_name, f.agent as fund_agent, f.contact as fund_contact from " + table_name + " as a left join pay_p2_fund as f on f.uuid = a.p2_fund where " + where_clause + " and p2_fund = $2 order by a.serial desc offset $3::int limit $4::int", ["%" + res.locals.keyword + "%", req.params.id, res.locals.offset, res.locals.page_rows], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 333
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            done();
            res.locals.uuid = req.params.id;
            return res.render('p2/fund/alter', {
              records: result.rows
            });
          });
        });
      };
    })(this));
  });

  router.all('/alter/export/:id', function(req, res, next) {
    var client, dirname, dnfile, done, err, line, lines, record, result, table_name, today, where_clause, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    dirname = settings.dnload_path + "/fund/alter";
    mkdirp.sync(dirname);
    today = moment().format('YYYY-MM-DD');
    dnfile = "" + dirname + "/fund_alter-" + today + ".txt";
    where_clause = "( a.amt::text like $1 or a.merno like $1 or a.termid like $1 or a.notes like $1 )";
    today = moment().format('YYYY-MM-DD');
    res.locals.start_date = req.body.start_date || req.query.start_date || today;
    res.locals.end_date = req.body.end_date || req.query.end_date || today;
    where_clause += "and a.ctime::date >= '" + res.locals.start_date + "'\nand a.ctime::date <= '" + res.locals.end_date + "'";
    table_name = 'pay_p2_fund_alter_hist';
    if (res.locals.start_date === today && res.locals.end_date === today) {
      table_name = 'pay_p2_fund_alter';
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 367
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
          });
          client.query("select a.*, f.name as fund_name, f.agent as fund_agent, f.contact as fund_contact from " + table_name + " as a left join pay_p2_fund as f on f.uuid = a.p2_fund where " + where_clause + " and p2_fund = $2 order by a.serial desc", ["%" + res.locals.keyword + "%", req.params.id], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 379
          }));
          __iced_deferrals._fulfill();
        })(function() {
          var _i, _len, _ref;
          if (err) {
            done(client);
            return next(err);
          }
          done();
          lines = [];
          _ref = result.rows;
          for (_i = 0, _len = _ref.length; _i < _len; _i++) {
            record = _ref[_i];
            line = "" + (record.ctime.format('YYYYMMDDHHmmss')) + ",";
            line += "" + (record.remains || '0') + ",";
            if (record.amt > 0) {
              line += "-" + record.amt + ",";
            } else {
              line += "" + (Math.abs(record.amt)) + ",";
            }
            line += "" + (record.merno || '') + ",";
            line += "" + (record.termid || '') + ",";
            line += "" + (record.fund_agent || '') + ",";
            line += "" + (record.notes || '');
            lines.push(line);
          }
          fs.writeFileSync(dnfile, lines.join("\n"));
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/home/qiwei/Desktop/luckpay-web/coffee/routes/p2/fund.coffee"
            });
            res.download(dnfile, path.basename(dnfile), __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  return err = arguments[0];
                };
              })(),
              lineno: 398
            }));
            __iced_deferrals._fulfill();
          })(function() {
            fs.unlink(dnfile);
            return res.end();
          });
        });
      };
    })(this));
  });

}).call(this);
